<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    /**
     * @return
     * 视频首页
     */
    public function index()
    {
        //分页展示
        $video = Video::orderby('id')->paginate(5);
        return view('admin.video_list')->with('video', $video);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    /**
     * @return void
     * 新增视频
     */
    public function create()
    {
        $oExpert = Expert::all();
        return view('admin.video_add')->with('oExpert', $oExpert);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    /**
     * @param Request $request
     * @return void
     * 新增视频处理逻辑
     */
    public function store(Request $request)
    {

        $data = $request->all();

        $rules = ['title' => 'required|min:3',
            'description' =>'required',
            'start_at'=>'required',
            'end_at'=>'required'];
        $message = [
            'title.required' => "标题不能为空",
            'title.min'=>"标题不能少于:min个字符",
            'description' => "描述不能为空",
            'start_at.required' => "开始时间不能为空",
            'end_at.required' => "结束时间不能为空" ];

        $validate = Validator::make($data, $rules, $message);
        if ($validate->fails())
        {
            $msg = $validate->messages()->first();
            return json_encode(['success' => false, 'msg' => $msg]);
        }


        Video::create($data);
        return json_encode(['success'=>true,'msg'=>'添加成功']);
    }


    /**
     * @return
     * 往期视频
     */
    public function review()
    {
        $video=Video::where('type','0')->paginate(6);
        return view('front.review')->with('video',$video);
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /**
     * @param $id
     * @return
     * 往期音频详情
     */
    public function show($id)
    {

        $oVideo=Video::where('id','=',$id)->get();
        return view('front.review_info')
            ->with('oVideo',$oVideo);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /**
     * @param $id
     * @return
     * 编辑视频
     */
    public function edit($id)
    {
        $oVideo= Video::find($id);
        $oExpert= Expert::all();
        return view('admin.video_edit')->with('oExpert', $oExpert)->with('oVideo', $oVideo);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /**
     * @param Request $request
     * @param $id
     * @return void
     * 编辑视频处理逻辑
     */
    public function update(Request $request, $id)
    {
        $video = Video::find($id);
        $data = $request->all();

        $rules = ['title' => 'required|min:3',
            'description' =>'required',
            'start_at'=>'required',
            'end_at'=>'required'
        ];
        $message = [
            'title.required' => "标题不能为空",
            'description.required' => "标题不能为空",
            'title.min'=>"标题不能少于:min个字符",
            'start_at.required' => "开始时间不能为空",
            'end_at.required' => "结束时间不能为空"
        ];
        $validate = Validator::make($data, $rules, $message);

        if ($validate->fails())
        {
            $msg = $validate->messages()->first();
            return json_encode(['success' => false, 'msg' => $msg]);
        }

    

        $video->update($data);
        return json_encode(['success'=>true,'msg'=>'修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /**
     * @param $id
     * @return void
     * 删除视频
     */
    public function destroy($id)
    {
        $video=Video::find($id);
        $video->delete();
        return json_encode(['msg'=>'删除成功']);



    }


}
